import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart' as getPack;
import 'package:smart_translator/app/services/DbService.dart';

import '../../../const/AppConst.dart';
import '../../../db/DbManager.dart';
import '../../../db/database.dart';
import '../../../event/DataCallBack.dart';
import '../../../nets/NetWorkManager.dart';
import '../../../routes/app_pages.dart';
import '../../../storage/StorageManager.dart';
import '../../../utils/tips.dart';
import '../../../views/controller/MineViewController.dart';
import '../../../widgets/CommonWidgets.dart';
import 'package:drift/drift.dart' as drift;
class MineProfileController extends GetxController {

var name=''.obs;
var email=''.obs;
var id='0'.obs;
var phone=''.obs;
var showEditModal = false.obs;
var avatar = ''.obs;
var localava = ''.obs; //上传后的本地照片路径



@override
  void onInit() {
    super.onInit();
    localava.value='';
    name.value = Get.arguments['name'] as String;
    email.value = Get.arguments['email'] as String;
    phone.value = Get.arguments['phone'] as String;
    id.value = Get.arguments['id'] as String;
    showEditModal.value = false;
    avatar.value =Get.arguments['avatar'] as String;;
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  void loginOut() {
    CommonWidgets.showConfirmDialog(Get.context!, '退出登录', '确定要退出登录吗?', '确定', '取消', DataCallBack(
        confirmCallBack: (v) {
          NetWorkManager.getInstance().requestBussiness(NetWorkManager.SYS_LOGOUT, {


          }, NetWorkManager.methodGET, DataCallBack(
              businessCallBack: (v) {
                print('login out success =========> ${v}');
                localava.value='';

                StorageManager.getStorage().remove(AppConst.LOGIN_TOKEN);
                Get.offAllNamed(Routes.LOGIN);

              },
              errorCallBack: (v) {
                Tips.showToast(v);
              }

          ),showLoading: true);
        },
        cancleCallBack: (v) {

        }
    ));

  }


  void deleteAccount() async {
    DbService service =  Get.find<DbService>();
    /*service.database!.into(service.database!.user).insert(UserCompanion.
    insert(email: '915270002@qq.com', phone: '111111', username: 'billyang', header_icon: 'aaaaaaa',
        access_token: 'access_token', refresh_token: 'refresh_token', expired_time: 1111,
        create_time: DateTime.now(), update_time: DateTime.now()));*/

    (service.database!.select(service.database!.user)..where((u)=> u.id.equals(8))).get().then((u) {
        print('uuuuuuu---------> ${u}');
        if (u.isNotEmpty) {
          (service.database!.update(service.database!.user)..where((u)=> u.username.equals('billyang'))).write(UserCompanion(
             phone: drift.Value('hihihihih')
          ));

        }

      });



  }


  void editName(realname) async {
  print('id $id.value');
    NetWorkManager.getInstance().requestBussiness(
        NetWorkManager.Edit_INFOS, {
      'id':id.value,
      'realname':realname
    }, NetWorkManager.methodPOST, DataCallBack(
        businessCallBack: (v) {
          print('editName ===========> $v');
          name.value = realname;
          final mineViewController= Get.find<MineViewController>();
          mineViewController.updateName(realname);
        },
        errorCallBack: (v) {
          Tips.showToast(v);
        }

    ));
  }

  void updateAvatar(newUrl){
    avatar.value = newUrl;
  }

  void updateLocalAvatar(url){
    localava.value = url;
  }
}
